package login.Controller.userInformation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import login.Model.dao.DBUtil;
import login.Model.dao.JsonUtil;
import login.Model.dao.UserDao;
import login.Model.entity.User;
import login.Model.service.UserService;
import login.Model.dao.JsonUtil;

import javax.mail.Session;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "loginServlet",value = "/login")
public class loginServlet extends HttpServlet{

    //实例化UseService对象
    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //转化成String类
        String jsonString=String.valueOf(JsonUtil.getJson(req));
        //转换成JSONObject类
        JSONObject jsonObject= JSON.parseObject(jsonString);
        //获取用户名，密码参数
        String name = jsonObject.getString("username");
        String pwd = jsonObject.getString("password");
        //获取验证码
        String code = jsonObject.getString("verifyCode");

        //put方法：设置一个json中的属性
        //status表示给前端的状态值
        PrintWriter out = resp.getWriter();
        JSONObject object = new JSONObject();

        /**
         * 判断非空
         */
        if(name==""){
            object.put("code","-1");
            object.put("message","用户名为空");
            out.println(object);
        }
        if(pwd==""){
            object.put("code","-1");
            object.put("message","密码为空");
            out.println(object);
        }
        if(code==""){
            object.put("code","-1");
            object.put("message","验证码为空");
            out.println(object);
        }


        /**
         * 验证码校验
         */
        //从Session中获取验证码
        HttpSession session = req.getSession();
        String verifiCode = (String) session.getAttribute("verifyCode");
        //返回json数据
        System.out.println(verifiCode);
        JSONObject res = new JSONObject();
        //比较
        if(!verifiCode.equals(code)){
            object.put("code","-1");
            object.put("message","验证码错误");
            out.println(object);
            return;
        }

        /**
         * 用户名密码校验
         */
        try {
            if(UserService.exit(name)){
                if(UserService.Judge(name,pwd)){
                    if(name=="admin"){
                        object.put("code","1");
                        object.put("message","欢迎"+name+"登录成功");
                        object.put("userType","root");
                        out.println(object);
                    }else{
                        object.put("code","1");
                        object.put("message","欢迎"+name+"登录成功");
                        object.put("userType","user");
                        out.println(object);
                    }
                }else{
                    object.put("code","-1");
                    object.put("message","密码错误");
                    out.println(object);
                }
            }else{
                object.put("code","-1");
                object.put("message","用户名不存在");
                out.println(object);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                DBUtil.freeConnection();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
